<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceGuestList extends core {

    public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
       
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit;
        $keyword = isset($_POST['keyword']) ? $_POST['keyword'] : "";
        $first_char = isset($_POST['continue_first_char']) ? $_POST['continue_first_char'] : "";
        $total = 0;
        if ($keyword !== "") {
            $keyword = $this->db->escape($keyword);
            $data = $this->db->get_results("SELECT * FROM guests WHERE CONCAT (firstname,' ', lastname) LIKE '%" . $keyword . "%' AND user_id={$user_id} AND is_invite = 1 ORDER BY firstname asc LIMIT $from,$limit;");
            $total = $this->db->get_row("SELECT count(guest_id) as total FROM guests WHERE CONCAT (firstname,' ', lastname) LIKE '%" . $keyword . "%' AND user_id={$user_id} AND is_invite = 1");
        } else {
            $data = $this->db->get_results("SELECT * FROM guests WHERE user_id={$user_id} AND is_invite = 1  ORDER BY firstname asc LIMIT $from,$limit;");
            $total = $this->db->get_row("SELECT count(guest_id) as total FROM guests WHERE user_id={$user_id} AND is_invite = 1 ;");
           }
        if (ceil($total->total / $limit) <= $page) {
            $end_paging = 1;
        } else {
            $end_paging = 0;
        }
        
        $html_render = '';
        
        foreach ($data as $guest) {
 
            $guest->firstname = $this->vn_str_filter($guest->firstname);
           $guest->lastname = $this->vn_str_filter($guest->lastname);
            if (strtoupper($guest->firstname[0]) != $first_char) {
                $html_render.='<li class="wd-abc">
                                <span class="wd-txt-abc">' . strtoupper($guest->firstname[0]) . '</span>
                        </li>';
                $first_char = strtoupper($guest->firstname[0]);
            }

            $html_render.='<li class="wd-list-item-guest">
                        <div class="wd-list-guest-ava">';
            if ($guest->photo != "") {
                
                $html_render.='<img src="' . $guest->photo . '" />';
            } else {
                
                $html_render.='<img src="img/no-ava.png" />';
            }
            
            $html_render.=' </div>
                        <div class="wd-list-guest-action">
                            <a href="editGuest.html" class="wd-edit-guest" rel="' . $guest->guest_id . '" onclick="editGuest(this)"><img src="img/edit-icon.png" /></a>
                            <span class="wd-delete-guest" rel="' . $guest->guest_id . '"><img src="img/delete-icon.png" /></span>
                        </div>
                        <div class="wd-list-guest-content">
                            <div class="wd-name-guest">' . $guest->firstname . ' ' . $guest->lastname . '</div>
                            <div class="wd-contact">' . $guest->address . '</div>';
            if ($guest->state == "" && $guest->zipcode == "") {
                $html_render.= '<div class="wd-contact">' . $guest->city . '</div>';
            } else {
                $html_render.= '<div class="wd-contact">' . $guest->city . ', ' . $guest->state . ' ' . $guest->zipcode . '</div>';
            }

            $html_render.=' <div class="wd-contact">' . $guest->phone . '</div>
                            <div class="wd-contact">' . $guest->email . '</div>
                        </div>
                    </li>';
          
        }
        
        if ($html_render == '') {
            $html_render = '<li class="wd-list-item-guest">No contact was found.</li>';
            $this->_JSONResponse(array('error' => 0, 'hide_icon_email'=>1 ,'html' => $html_render, 'page' => $page + 1, 'continue_first_char' => $first_char, 'keyword' => $keyword, 'end_paging' => $end_paging));
        }else{
            $this->_JSONResponse(array('error' => 0,'hide_icon_email'=>0 , 'html' => $html_render, 'page' => $page + 1, 'continue_first_char' => $first_char, 'keyword' => $keyword, 'end_paging' => $end_paging));
        }
        
        
    }
  
     function vn_str_filter ($str){
        $unicode = array(
            'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
            'd'=>'đ',
            'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'i'=>'í|ì|ỉ|ĩ|ị',
            'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
			'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'D'=>'Đ',
            'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
            'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        
       foreach($unicode as $nonUnicode=>$uni){
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
       }
		return $str;
    }
}

$processor = new webServiceGuestList();
$processor->process();
?>